package appli.launcher;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import system.DebugOptionInterface;
import system.util.Loader;
import appli.agentImpl.Store;
import appli.product.Product;

/**
 * Start a new Store agent
 */
public class LauncherStore implements DebugOptionInterface {

	/**
	 * @param args {agentName, host, port}
	 */
	public static void main(String[] args) {
		try {
			// chargement du code de l'agent
			Loader l = new Loader(Store.class.getName());
			Class<Store> c = l.loadClass(Store.class.getName(), true);
			//we take the constructor with following parameters : 
			//String agentName, String host, int port, List<Product> products
			Constructor<Store> constructor = c.getConstructor(new Class[]{String.class, String.class, int.class, List.class});
			String agentName = args[0];
			String host = args[1];
			int port = Integer.parseInt(args[2]);
			List<Product> products = LauncherStore.getListProducts(agentName);
			Store s = constructor.newInstance(new Object[]{agentName, host, port, products});
			
			if (OPTION) { 
				System.out.println("Magasin démarré sur : " + s.getCurrentHost() + ":" + s.getCurrentPort());
				System.out.println("Liste des produits vendus : ");
				for(Product p : products)
					System.out.println("\tnom = " + p.getName() + ", prix = " + p.getPrice() + ", quantité = " + p.getQuantity());
			}
			s.main();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * this method is used to test different product list for different stores
	 * @param storeName
	 * @return
	 */
	public static List<Product> getListProducts(String storeName) {
		List<Product> products = new ArrayList<Product>();
		//TODO : upgrade this (parsing of an XML file ?)
		if (storeName.equals("Carrefour")) {
			products.add(new Product("caviar", 3, 50));
			products.add(new Product("foie gras", 10, 25));
			products.add(new Product("beaujolais nouveau", 20, 10));
			products.add(new Product("tartiflette", 4, 30));
			products.add(new Product("olives", 5, 2));
		}
		else if (storeName.equals("Casino")) {
			products.add(new Product("caviar", 2, 45));
			products.add(new Product("tartiflette", 4, 20));
			products.add(new Product("raclette", 10, 10));
			products.add(new Product("fondue", 20, 5));
		}
		else if (storeName.equals("ED")) {
			products.add(new Product("olives", 5, 3));
			products.add(new Product("caviar", 4, 25));
			products.add(new Product("cornichon", 10, 2));
			products.add(new Product("soupe aux choux", 20, 1));
		}
		return products;
	}
}
